package com.snow.useviewdemo.function.phonesystem;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.databinding.ActivityPhoneAppsBinding;
import com.snow.useviewdemo.util.PhoneAppUtils;
import com.snow.useviewdemo.util.TextViewUtils;

import java.util.List;

/**
 * Created by zhaoyingtao
 * Date: 2020/7/18
 * Describe:获取手机安装所有应用的信息
 */
public class PhoneAppsActivity extends BaseActivity<ActivityPhoneAppsBinding> {
    private final int DELAYEDTIME = 1000;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone_apps;
    }

    List<PhoneAppInfoBean> appInfoBeanList;
    private int currentPosition = 0;

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        appInfoBeanList = PhoneAppUtils.init().scanLocalInstallAppList(getPackageManager());
        ToastUtils.getInstance().showToast("应用数："+appInfoBeanList.size());
        handler.sendEmptyMessageDelayed(101, 2000);
    }

    private void animation() {
        mBinding.imgAnimation.clearAnimation();
        AnimatorSet animatorSet = new AnimatorSet();
        // 获得当前按钮的位置
        ObjectAnimator translationY = ObjectAnimator.ofFloat(mBinding.imgAnimation, "translationY", 0, -TextViewUtils.init().dp2px(this, 300));
        //透明度动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mBinding.imgAnimation, "alpha", 1, 0.1f);
        animatorSet.setDuration(DELAYEDTIME);
        animatorSet.play(translationY).with(alpha);
        animatorSet.start();
    }

    /**
     * Android解析apk包信息的方法
     */
    private void getApkInfoFromApk() {
        String path = "/storage/emulated/0/Android/data/com.test.demo/cache/com_qq_e_download/apk/com.mg.ggvideo.apk";
        PackageManager pm = getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            String appName = String.valueOf(pm.getApplicationLabel(appInfo));//获取的并不是应用名(安装后可以获取到)
            String packageName = appInfo.packageName;  //得到安装包名称
            String version = info.versionName;       //得到版本信息
            Drawable icon = pm.getApplicationIcon(appInfo);//得到图标信息
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //避免内存泄漏
        handler.removeCallbacksAndMessages(null);
        handler = null;
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 101:
                    if (currentPosition < appInfoBeanList.size()) {
                        PhoneAppInfoBean infoBean = appInfoBeanList.get(currentPosition);
                        mBinding.textView.setText(infoBean.getAppName());
                        mBinding.textViewPackage.setText(infoBean.getAppPackage());
                        mBinding.imageView.setImageDrawable(infoBean.getAppIcon());
                        mBinding.imgAnimation.setImageDrawable(infoBean.getAppIcon());
                        animation();
                        if (currentPosition < appInfoBeanList.size() - 1) {
                            PhoneAppInfoBean infoBeans = appInfoBeanList.get(currentPosition + 1);
                            mBinding.textView.setText(infoBeans.getAppName());
                            mBinding.textViewPackage.setText(infoBeans.getAppPackage());
                            mBinding.imageView.setImageDrawable(infoBeans.getAppIcon());
                        } else {
                            mBinding.imageView.setVisibility(View.GONE);
                        }
                    } else {
                        handler.removeCallbacksAndMessages(null);
                    }
                    currentPosition++;
                    handler.sendEmptyMessageDelayed(101, DELAYEDTIME);
                    break;
            }
            return false;
        }
    });
}
